package future;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author fei
 * @date 2023/1/4 11:37
 * @description future中isDone 轮询
 */
public class FutureAPIDemo2 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(() -> {
            System.out.println(Thread.currentThread().getName() + "\t ------comt in");
            // 暂停几秒钟线程
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "task over";
        });

        Thread thread = new Thread(futureTask, "t1");
        thread.start();

        System.out.println(Thread.currentThread().getName() + "\t ----忙其它任务了");

        while (true) {
            if (futureTask.isDone()) {
                System.out.println(futureTask.get());
                break;
            } else {
                // 暂停毫秒
                try{ TimeUnit.MILLISECONDS.sleep(500);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("正在处理中，不要再催了，再催熄火！");
            }
        }
    }

}
